package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.IdeaComment;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.RowMapper;

public class IdeaCommentDao {
	
	private DBHelp<IdeaComment> db = new DBHelp<IdeaComment>();
	
	public void save(IdeaComment ic){
		String sql="insert into t_idea_comment (id,content,createtime,userId,ideaId,projectId) values(?,?,?,?,?,?)";
		db.executeSQL(sql, ic.getId(),ic.getContent(),ic.getCreatetime(),ic.getUserId(),ic.getIdeaId(),ic.getProjectId());
	}

	public void update(IdeaComment ic){
		String sql="update t_idea_comment set content=?,createtime=?,userId=?ideaId=?,projectId=?where id=?";
		db.executeSQL(sql, ic.getContent(),ic.getCreatetime(),ic.getUserId(),ic.getIdeaId(),ic.getProjectId(),ic.getId());
	}
	
	public List<IdeaComment> findByIdeaId(int ideaId){
		String sql = "select id,content,createtime,userId,projectId from t_idea_comment where ideaId=?";
		return db.executeQueryToList(sql, new IdeaCommentRowMapper(),ideaId);
	}
	
	
	public void removeByIdeaId(int ideaId) {
		String sql="delete from t_idea_comment where ideaId=?";
		db.executeSQL(sql,ideaId);
	}
	
	private class IdeaCommentRowMapper implements RowMapper<IdeaComment>{
		public IdeaComment mapRow(ResultSet rs) throws SQLException {
			IdeaComment ic = new IdeaComment();
			
			ic.setId(rs.getInt("id"));
			ic.setContent(rs.getString("content"));
			ic.setCreatetime(rs.getString("createtime"));
			ic.setUserId(rs.getInt("userId"));
			ic.setIdeaId(rs.getInt("ideaId"));
			ic.setProjectId(rs.getInt("projectId"));
			
			return ic;
		}
	}
	
}
